package com.jsn.filter;


import com.jsn.utils.IpUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AccessInFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {

        String ip = IpUtils.getIP(exchange.getRequest());
//        System.out.println("ip:"+ip);
        // 该步骤可选。可以传递给下游服务器，用于业务处理
        ServerHttpRequest request = exchange.getRequest().mutate()
                .header("CLIENT_REAL_IP", new String[]{ip})
                .build();
        return chain.filter(exchange.mutate().request(request).build());
    }


    @Override
    public int getOrder() {

        return -3;
    }
}
